import http from '../utils/http'
import cache from '../utils/Cache'
import config from '../config/uri'

/**
 * 获取swiper数据
 * @param int page 分页码  
 */
export const getSwiper = (page = 1) => {
  return http({
    url: config.swiper
  })
}

/**
 * 获取新闻列表
 * @param int page 分页码  
 */
export const getNews = (page = 1) => {
  return http({
    url: config.news,
    data: { page }
  })
}


/**
 * 获取新闻详情
 * @param int id 新闻id  
 */
export const getDetail = (id = 0) => {
  return http({
    url: config.news + '/' + id
  })
}

// 添加浏览记录 PV
export const addRecord = article_id => {
  let user_id = cache.get('userid')
  return http({
    url: config.addRecord,
    data: { user_id, article_id },
    method: 'POST'
  })
}

// 添加浏览记录 PV
export const insertFav = article_id => {
  let user_id = cache.get('userid')
  article_id *= 1

  return http({
    url: config.addFavUri,
    data: { user_id, article_id },
    method: 'POST'
  })
}

// 检查用户是否收藏过此文章
export const checkFav = article_id => {
  let user_id = cache.get('userid')
  article_id *= 1

  return http({
    url: config.hasfav,
    data: { user_id, article_id }
  })
}

// 搜索
export const searchfn = kw => {
  return http({
    url: config.search,
    data: { kw }
  })
}

